package com.avic.szzx.util;

import com.avic.szzx.controller.bean.quality_tracing.EquipmentInformationCorrespondence;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Field;
import java.util.*;

@Configuration
public class EsUtils {

    private static HashSet usedNumbers = new HashSet<>();
    /**
     * 将List<Object>转换为List<Map<String,Object>>
     * @param list
     * @return
     */
    public static List<LinkedHashMap<String,Object>> listObjToMap(List<Object> list) {
        List<LinkedHashMap<String, Object>> maps = new ArrayList<>();
        for (Object obj : list) {
            Class c = obj.getClass();
            Field[] f = c.getDeclaredFields();
            LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            for (Field fie : f) {
                try {
                    //反射知识
                    fie.setAccessible(true);//取消语言访问检查
                    map.put(fie.getName(), fie.get(obj));//获取私有变量值
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            //获取父类的私有属性
            for (Field fie : c.getSuperclass().getDeclaredFields()) {
                try {
                    fie.setAccessible(true);//取消语言访问检查
                    map.put(fie.getName(), fie.get(obj));//获取私有变量值
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            maps.add(map);
        }
        return maps;
    }

    /**
     * 将List<Object>转换为List<Map<String,Object>>
     * @param obj
     * @return
     */
    public static LinkedHashMap<String,Object> ObjToMap(Object obj) {
            Class c = obj.getClass();
            Field[] f = c.getDeclaredFields();
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            for (Field fie : f) {
                try {
                    //反射知识
                    fie.setAccessible(true);//取消语言访问检查
                    map.put(fie.getName(), fie.get(obj));//获取私有变量值
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            //获取父类的私有属性
            for (Field fie : c.getSuperclass().getDeclaredFields()) {
                try {
                    fie.setAccessible(true);//取消语言访问检查
                    map.put(fie.getName(), fie.get(obj));//获取私有变量值
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        return map;
    }

    public static String getId() {
        int randomNumber;
        do {
            randomNumber = new Random().nextInt(10000000);
        } while (usedNumbers.contains(randomNumber));
        usedNumbers.add(randomNumber);
        return String.valueOf(randomNumber);
    }
}
